
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       xrce_dds_topic_common.c
  * @author     baiyang
  * @date       2022-4-10
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "xrce_dds_topic_common.h"

#include <xrce_dss/xrce_dds.h>

#include <rtthread.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/

/*----------------------------------variable----------------------------------*/
extern XrceDDS xrce_dds;
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
/**
  * @brief       创建用于收发topic的实体
  * @param[in]   object_id  
  * @param[in]   topic_name  
  * @param[in]   type_name  
  * @param[in]   datareader_id  
  * @param[in]   datawriter_id  
  * @param[in]   qos  
  * @param[out]  
  * @retval      
  * @note        object_id：每个topic的object_id时唯一的，
  *              否则后面的会把前面的刷掉。
  *              datareader_id为NULL时，只接收topic不发送，
  *              datawriter_id为NULL时，只发送topic不接收，
  *              一般情况下，同一topic不要同时启用发送和接
  *              收，这样会收到自己发送的topic。
  */
void xrce_dds_topic_create_entities(uint16_t object_id,
                                      const char* topic_name,
                                      const char* type_name,
                                      uxrObjectId* datareader_id,
                                      uxrObjectId* datawriter_id,
                                      uxrQoS_t* qos,
                                      uxrDeliveryControl* delivery_control
                                      )
{
    uxrQoS_t qos_default = {
        .reliability = UXR_RELIABILITY_BEST_EFFORT, .durability = UXR_DURABILITY_TRANSIENT_LOCAL,
        .history = UXR_HISTORY_KEEP_LAST, .depth = 0
    };

    if (qos != NULL) {
        qos_default = *qos;
    }

    // Create entities
    uxrObjectId topic_id = uxr_object_id(object_id, UXR_TOPIC_ID);
    uint16_t topic_req = uxr_buffer_create_topic_bin(&xrce_dds.xrce_dds_session, xrce_dds.besteffort_out, topic_id, xrce_dds.participant_id,
                    topic_name,
                    type_name, UXR_REPLACE);

    if (datareader_id != NULL) {
        *datareader_id = uxr_object_id(object_id, UXR_DATAREADER_ID);
        uint16_t datareader_req = uxr_buffer_create_datareader_bin(&xrce_dds.xrce_dds_session, xrce_dds.besteffort_out, *datareader_id, xrce_dds.subscriber_id,
                        topic_id, qos_default, UXR_REPLACE);
    }

    if (datawriter_id != NULL) {
        *datawriter_id = uxr_object_id(object_id, UXR_DATAWRITER_ID);
        uint16_t datawriter_req = uxr_buffer_create_datawriter_bin(&xrce_dds.xrce_dds_session, xrce_dds.besteffort_out, *datawriter_id, xrce_dds.publisher_id,
                        topic_id, qos_default, UXR_REPLACE);
    }

    // Request topics
    uxrDeliveryControl delivery_control_default = {
        0
    };
    delivery_control_default.max_samples = UXR_MAX_SAMPLES_UNLIMITED;

    if (delivery_control != NULL) {
        delivery_control_default = *delivery_control;
    }

    if (datareader_id != NULL) {
        uxr_buffer_request_data(&xrce_dds.xrce_dds_session, xrce_dds.besteffort_out, *datareader_id, xrce_dds.besteffort_in,
                &delivery_control_default);
    }

    uxr_run_session_time(&xrce_dds.xrce_dds_session, 0);

    // 延时5毫秒，使串口发送线程有时间将xrc dds客户端的请求发送到代理端
    rt_thread_mdelay(5);
}

/*------------------------------------test------------------------------------*/

